package com.smiier.skin;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.skinapp.R;

import com.smiier.skin.constant.Constant;
import com.smiier.skin.extra.DoctorReserveDetailExtra;
import com.smiier.skin.extra.QuestionAnswerExtra;
import com.smiier.skin.net.AnswerAppraiseSetTask;
import com.smiier.skin.net.AnswerAppraiseSetTask.AnswerAppraiseSetRequest;
import com.smiier.skin.net.AnswerAppraiseSetTask.AnswerAppraiseSetResponse;
import com.smiier.skin.net.DoctorReserveDetailTask.DoctorReserveDetail;
import com.smiier.skin.net.TreatmentProjectCommentsTask;
import com.smiier.skin.net.TreatmentProjectCommentsTask.TreatmentProjectCommentsRequest;
import com.smiier.skin.net.TreatmentProjectCommentsTask.TreatmentProjectCommentsResponse;
import com.smiier.skin.net.entity.QuestionAnswer;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.umeng.analytics.MobclickAgent;

public class CreateProjectCommentActivity extends BasicActivity {
	EditText mComment;
	CheckBox mGoodComment;
	TextView mSubmit;
	DoctorReserveDetailExtra extra = new DoctorReserveDetailExtra();
	DoctorReserveDetail mDetail;
	QuestionAnswer mQuestion;
	QuestionAnswerExtra mQuestionExtra = new QuestionAnswerExtra();
	boolean isTreatment = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent i = getIntent();
		if (extra.getFrom(i)) {
			isTreatment = true;
			mDetail = extra.getUser();
		}

		if (mQuestionExtra.getFrom(i)) {
			isTreatment = false;
			mQuestion = mQuestionExtra.getQuestionAnswer();
		}

		setContentView(R.layout.activity_create_project_comment);
		init();
		setNavTitle("创建评价");
		setNavLeftBtn("取消");
		mComment = findViewById(R.id.reserve_comment, EditText.class);
		mGoodComment = findViewById(R.id.good_comment, CheckBox.class);
		mSubmit = findViewById(R.id.submit_comment, TextView.class);
		mSubmit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String comment = mComment.getText().toString();
				boolean good = mGoodComment.isChecked();
				if (comment == null || comment.trim().equals("")) {
					toast("请输入评论");
					return;
				}
				if (isTreatment) {
					TreatmentProjectCommentsTask task = new TreatmentProjectCommentsTask();
					TreatmentProjectCommentsRequest req = new TreatmentProjectCommentsRequest();
					req.comments = comment;
					if (good) {
						req.commentslevel = "是";
					} else {
						req.commentslevel = "不是";
					}
					req.treatmentprojectid = mDetail.id + "";
					req.userid = mDetail.doctor_id + "";
					task.setRequest(req);
					task.addListener(new NetTaskListener<TreatmentProjectCommentsTask.TreatmentProjectCommentsRequest, TreatmentProjectCommentsTask.TreatmentProjectCommentsResponse>() {

						@Override
						public void onException(
								INetTask<TreatmentProjectCommentsRequest, TreatmentProjectCommentsResponse> task,
								Exception e) {
							e.printStackTrace();
						}

						@Override
						public void onComplete(
								INetTask<TreatmentProjectCommentsRequest, TreatmentProjectCommentsResponse> task,
								TreatmentProjectCommentsResponse response) {
							if (response != null && response.ResultCode == 200) {
								toast("评论成功");
							} else {
								toast("评论失败");
							}
						}
					});
					add(task);
				} else {
					AnswerAppraiseSetTask task = new AnswerAppraiseSetTask();
					AnswerAppraiseSetRequest req = new AnswerAppraiseSetRequest();
					req.Token = GlobalSettings.sToken;
					req.qid = mQuestion.Question.Qid;
					if (good) {
						req.level = 3;
					}
					req.content = comment;
					// req.PayPwd = OUtil.md5(pay_password);
					task.setRequest(req);
					task.addListener(new NetTaskListener<AnswerAppraiseSetTask.AnswerAppraiseSetRequest, AnswerAppraiseSetTask.AnswerAppraiseSetResponse>() {

						@Override
						public void onException(
								INetTask<AnswerAppraiseSetRequest, AnswerAppraiseSetResponse> task,
								Exception e) {

						}

						@Override
						public void onComplete(
								INetTask<AnswerAppraiseSetRequest, AnswerAppraiseSetResponse> task,
								AnswerAppraiseSetResponse response) {
							if (!CommonUtility.isNull(response)
									&& response.ResultCode == 200) {
								CommonUtility.toast(activity, "提交评价成功");
								Intent intent = new Intent(
										Constant.RECIVER_COMMENT);
								if (mQuestionExtra.putTo(intent)) {
									sendBroadcast(intent);
								}
								intent = new Intent(Constant.RECIVER_CHAT_LIST);
								sendBroadcast(intent);
								CommonUtility
										.finishActivityFromName(ChatActivity.class
												.getSimpleName());
								finish();
							} else {
								CommonUtility.toast(activity, "提交评价失败");
							}
						}
					});
					add(task);
				}
			}
		});
	}
	
	/*@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
}
